/*
 Cogumelo v0.1 - INNOTO
Copyright (C) 2010 Innoto Gestión para el Desarrollo Social S.L. <mapinfo@innoto.es>

This program is free software; you can redistribute it and/or
modify it under the2009 terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
USA.
*/

var formMap = Class.create
({
	
	init: function(mapObj, opts)
	{
		this.map = mapObj;
		this.newMarker = false;
		
		
		// Default map options
		this.options = new Object({
				icon: false, // map icon to add or move
				
				// element id's
				form_id:false,
				lat_input_id: false,
				lng_input_id: false,
				initzoom_id: false,
				// element values
				lat_input_value: '',
				lng_input_value: '',
				initzoom_value: ''
		});
		
		$.extend(true, this.options, opts);
		
		// add form elements if not exist
		this.createformElements();

	},

	// create form imputs into form
	createformElements: function(){
		if(this.options.form_id != false){

			// add latitude input
			if($('#'+this.options.lat_input_id).length == 0 ){
					$('#'+this.options.form_id).append('<input id="'+ this.options.lat_input_id +'" style="display:none;" value="'+ this.options.lat_input_value +'"/>');
			}
            else
                this.options.lat_input_value = $('#'+this.options.lat_input_id).val();
			// add longitude input
			if($('#'+this.options.lng_input_id).length == 0)
					$('#'+this.options.form_id).append('<input id="'+ this.options.lng_input_id +'" style="display:none;" value="'+ this.options.lng_input_value +'"/>');
            else
                this.options.lng_input_value = $('#'+this.options.lng_input_id).val();
			// add zoom input
			if($('#'+this.options.initzoom_id).length == 0)
				$('#'+this.options.form_id).append('<input id="'+ this.options.initzoom_id +'" style="display:none;"  value="'+ this.options.initzoom_value +'"/>');
            else
                this.options.initzoom_value = $('#'+this.options.initzoom_id).val();

		}
		else
			alert('The form id is not defined');
	},

	clickPoint: function() {
		var that = this
		
		// waiting for map click to create new marker
		var NewMarker = GEvent.addListener(this.map.map , "click", function(overlay, latlng) {
			
			that.addPoint(latlng);
			GEvent.removeListener(NewMarker);

		});
	},
	
	//
	//
	addPoint: function(latlng) {
		that = this;
		// if exist a specific icon use it, else use defautl
		if(that.options.icon != false)
			that.newMarker = new GMarker(latlng, {icon: that.options.icon.icon, draggable: true});
		else
			that.newMarker = new GMarker(latlng, {icon:false, draggable: true});
		
		that.map.map.addOverlay(that.newMarker);

		// add marker drag event
		GEvent.addListener(that.newMarker, "drag", function() {	that.updateInputs();});
		
		// refresh input data
		that.updateInputs();
	},
	
	// Draw into hide inputs the marker latlng and map zoom level 
	updateInputs: function() 
	{
		// Zoom and marker data
		$('#'+this.options.lat_input_id).attr("value", this.newMarker.getLatLng().lat() );
		$('#'+this.options.lng_input_id).attr("value", this.newMarker.getLatLng().lng() );
		$('#'+this.options.initzoom_id).attr("value", this.map.map.getZoom() ) 
	},
	
	// init map trigerts
	execute: function()
	{
		
		if(this.options.lat_input_value != '' && this.options.lng_input_value != '') {
			// add map points
			var pointLatLng = new GLatLng(this.options.lat_input_value , this.options.lng_input_value);
			this.addPoint(pointLatLng);
		}
		else {
			// wait for map click to add icon
			this.clickPoint();
		}
		
		//set center on map point
		if(this.newMarker != false)
            this.map.map.setCenter(this.newMarker.getLatLng());
	}
});

